package com.pengtu.gsj.service.impl.check;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.CheckProjectDao;
import com.pengtu.gsj.entity.CheckResultVo;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.check.CheckProject;
import com.pengtu.gsj.service.check.CheckProjectService;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class CheckProjectServiceImpl implements CheckProjectService {
	
	@Autowired
	private CheckProjectDao checkProjectDao;
	
	@Autowired
	private SysmenuManagerService sysmenuManagerService;

	@Override
	public Page<CheckProject> getCheckProjects(Page<CheckProject> page,
			QueryFilter queryFilter) {
		return checkProjectDao.findPage(page, queryFilter);
	}

	@Override
	public CheckProject getCheckProject(String id) {
		return checkProjectDao.get(id);
	}

	@Override
	public boolean isprojectNameUnique(String newProjectName,
			String oldProjectName) {
		return checkProjectDao.isPropertyUnique("projectName", newProjectName, oldProjectName);
	}

	@Override
	public void save(CheckProject checkProject) {
		checkProjectDao.save(checkProject);
	}

	@Override
	public void delete(CheckProject checkProject) {
		checkProjectDao.delete(checkProject);
	}

	/**
	 * 
	 * getStatusCheckProjects:
	 * 适用:判断系统中是否有未发布的国省检项目
	 * @param countryOrProvince 国省检项目区分标志  0 国检 1 省检
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public boolean getStatusCheckProjects(String countryOrProvince) {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("countryOrProvince", countryOrProvince, MatchType.EQ);
		queryFilter.addFilter("status", "0", MatchType.EQ);
		if (checkProjectDao.find(queryFilter).size()>0) {
			return false;
		} else {
			return true;
		}
		
	}
	
	public List<CheckProject> getCheckProjects(QueryFilter queryFilter){
		return checkProjectDao.find(queryFilter);
	}

	@Override
	public List<CheckProject> getCheckProjectsByType(String type) {
		QueryFilter queryFilter = new QueryFilter();
		queryFilter.addFilter("countryOrProvince", type, MatchType.EQ);
		queryFilter.addFilter("status", "0", MatchType.EQ);
		return checkProjectDao.find(queryFilter);
	}

	@Override
	public List<Map<String, Object>> getTotalCheckResult(String projectId) {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<Sysmenu> sysmenus = sysmenuManagerService.getCheckModule(projectId);
		double totalTableFull = 0.0;
		double totalTestFull = 0.0;
		for (Sysmenu sysmenu : sysmenus) {
			List<CheckResultVo> checkResultVos = sysmenuManagerService.getCheckResultVoByParentId(projectId, sysmenu.getId());
			if (checkResultVos.size()>0) {
				CheckResultVo checkResultVo = checkResultVos.get(0);
				map = new HashMap<String, Object>();
				map.put("name", sysmenu.getName());
				if (checkResultVo.getTableFull() == null) {
					checkResultVo.setTableFull(0.0);
				}
				map.put("tableFull", checkResultVo.getTableFull());
				if (checkResultVo.getTestFull() == null) {
					checkResultVo.setTestFull(0.0);
				}
				map.put("testFull", checkResultVo.getTestFull());
				if (checkResultVo.getPerfection() == null) {
					checkResultVo.setPerfection(0.0);
				}
				map.put("perfection", MathsUtils.formatDouble(checkResultVo.getPerfection()*100, 2));
				list.add(map);
				totalTableFull += checkResultVo.getTableFull();
				totalTestFull += checkResultVo.getTestFull();
			}
		}
		map = new HashMap<String, Object>();
		map.put("name", "总体");
		map.put("tableFull", totalTableFull);
		map.put("testFull", totalTestFull);
		map.put("perfection", MathsUtils.divDouble(totalTestFull*100, totalTableFull, 2));
		list.add(map);
		return list;
	}

}
